package com.jsonal.controller.manager;

import com.jsonal.controller.BaseController;
import com.jsonal.entity.api.ArticleBean;
import com.jsonal.form.api.ArticleForm;
import com.jsonal.service.ArticleService;
import com.jsonal.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: jingji.lin
 * @Description: 后台文章管理
 * @Date: 2020/6/20 15:19
 * @Version: 1.0
 */
@Controller
public class ArticleManagerController extends BaseController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping(value = "/managers/getArticleList", method = RequestMethod.GET)
    @ApiOperation(value = "/managers/getArticleList", httpMethod = "GET", notes = "获取文章列表")
    @ResponseBody
    public Result getArticleList(ArticleForm articleForm) {
        ArticleBean bean = new ArticleBean();
        this.setFormToBean3(articleForm, bean);
        return articleService.getArticleList(bean);
    }

    @RequestMapping(value = "/managers/remove/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "/managers/remove/{id}", httpMethod = "POST", notes = "根据 id 删除数据")
    @ResponseBody
    public Result remove(@PathVariable String id) {
        if (id == null || "".equals(id)) {
            return new Result(false, "id不能为空");
        }
        return articleService.remove(id);
    }

    @RequestMapping(value = "/managers/getBlogInfo/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "/managers/getBlogInfo/{id}", httpMethod = "POST", notes = "查看详情")
    @ResponseBody
    public Result getBlogInfo(@PathVariable String id) {
        if (id == null || "".equals(id)) {
            return new Result(false, "id不能为空");
        }
        return articleService.getBlogInfo(id);
    }

    @RequestMapping(value = "/managers/updateInfo", method = RequestMethod.POST)
    @ApiOperation(value = "/managers/updateInfo", httpMethod = "POST", notes = "修改数据")
    @ResponseBody
    public Result updateInfo(@RequestBody ArticleForm articleForm) {
        ArticleBean bean = new ArticleBean();
        this.setFormToBean3(articleForm, bean);
        return articleService.updateInfo(bean);
    }

    @RequestMapping(value = "/managers/hot/{id}", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "/managers/hot/{id}", httpMethod = "POST", notes = "热门文章")
    public Result hot(@PathVariable String id) {
        if ("".equals(id) || id == null) {
            return new Result(false, "id 不能为空");
        }
        return articleService.hot(id);
    }

    @RequestMapping(value = "/managers/hotArticle", method = RequestMethod.GET)
    @ApiOperation(value = "/managers/hotArticle", httpMethod = "GET", notes = "获取热门文章")
    @ResponseBody
    public Result hotArticle() {
        return articleService.getHotList();
    }
}
